<template>
  <div class="tree-item ">
    <div v-if="deep>0" class="item-point-line" :style="{left:deep*w-(w/2)+p+'px'}" />
    <div class="tree-item-title" :style="{paddingLeft:(w*deep)+p+'px'}" @click="tree.isOpen=!tree.isOpen">
      <i v-if="tree.children" class="fa" :class="tree.isOpen?'fa-caret-down':'fa-caret-right'" />
      <slot :node="tree">
        <span class="fa " :class="tree.children?'fa-folder-o':'fa-file-text-o'" />
        <span class="tree-text">{{ tree.name }}_{{ deep }}</span>
      </slot>
    </div>
    <div v-if="tree.children" v-show="tree.isOpen" class="tree-list">
      <div class="list-point-line" :style="{left:deep*w+(w/2)+p+'px'}" />
      <Tree v-for="(item,i) in tree.children" :key="i" :tree="item" :deep="deep+1">
        <template slot-scope="{node}">
          <slot :node="node" />
        </template>
      </Tree>
    </div>
  </div>
</template>
<script>
export default {
  name: 'Tree',
  props: {
    tree: {
      default() {
        return {
          name: '文件夹',
          type: 'file'
        }
      }
    },
    deep: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      w: 20,
      p: 10
    }
  }
}
</script>

<style lang="less" scoped>
  .tree-item,.tree-list{
    position: relative;
  }
  .tree-item-title{
    height: 30px;
    line-height: 30px;
    padding: 0 10px;
    font-size: 14px;
    cursor: pointer;
    color: #858585;
    overflow: hidden;
    word-wrap: normal;
    white-space: nowrap;
    text-overflow: ellipsis;
    vertical-align: middle;
  }
  .tree-item-title i{
    display: inline-block;
    width: 20px;
    line-height: 30px;
    text-align: center;
    font-size: 20px;
    color: #a1a1a1;
  }
  .tree-item-title span.fa{
    display: inline-block;
    width: 20px;
    line-height: 30px;
    text-align: center;
  }
  .tree-item-title span.tree-text{
    display: inline-block;
    line-height: 30px;
  }
  .tree-item-title:hover{
    background-color: #f2f2f2;
  }
  .list-point-line{
    position: absolute;
    display: block;
    width: 1px;
    border-left: 1px dotted #333333;
    left: 10px;
    bottom: 15px;
    top: -5px;
    z-index: 99;
    margin-left: -1px;
  }
  .item-point-line{
    position: absolute;
    display: block;
    height: 1px;
    width: 10px;
    border-top: 1px dotted #333333;
    left: 10px;
    bottom: 15px;
    top: 15px;
  }
</style>
